// pages/buy/discounts/discounts.js
let api = require('../../../api/api.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (data) {
    console.log(data)
    api.choosecoupon(data, res => {
      console.log(res)
      res.data.result.forEach(function(element){
        if(element.id == data.id){
          element.checked = true
        }else{
          element.checked = false
        }
      })
      this.setData({
        title: res.data.result
      })
    })
  },

  radioChange:function(e){
    console.log(e.detail.value)
    var full_price = 0;
    var discountPrice = 0
    this.data.title.forEach(function(element){
      if(element.id == e.detail.value){
        full_price = element.full_price
      }
    })
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2] //上一个页面
    var createData = prevPage.data['createData']
    prevPage.data['createData'].coupon_price = full_price
    prevPage.data['createData'].coupon_id = e.detail.value
    var value = prevPage.data['checked']
    if (value) {
      discountPrice = parseFloat(createData.totalprice) - parseFloat(createData.coupon_price) - parseFloat(createData.user_price)
    } else {
      discountPrice = parseFloat(createData.totalprice) - parseFloat(createData.coupon_price)
    }
    prevPage.data['discountPrice'] = discountPrice < 0 ? 0 : discountPrice

    prevPage.setData(prevPage.data)
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})